import java.util.Arrays;

public class Loading {
    /**
     * 最优装载问题
     * 题目：有一批集装箱要装上一艘载重量为c的轮船。其中
     * 集装箱i的重量为Wi。最优装载问题要求确定在装载体    积不受限制的情况下，将尽可能多的集装箱装上轮船
     * 思路：从小到大进行排列，尽可能多地装入集装箱
     */
    public static void main(String[] args) {
        int []weights={2,1,5,3,2,4,8,6,5,23,14,6,37,8,23,45,12,7,29};
        int capacity=100;
        System.out.println("总可以装载的集装箱个数为： "+maxBoxes(weights,capacity));
    }
    public static int maxBoxes(int[] weights,int capacity){
        Arrays.sort(weights);
        int count=0;
        int currCapacity=0;
        for(int weight:weights){
            if(currCapacity+weight<=capacity){
                count++;
                currCapacity+=weight;
                System.out.print(weight+",");
            }else{
                System.out.println("\n最后可以装载的容量为："+currCapacity);
                break;
            }
        }
        return count;
    }
}
